package com.nnfe.fyd.feature.inquiry;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.nnfe.fyd.BusKey;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.BaseSwipeRefreshFragment;
import com.nnfe.fyd.base.MyBaseAdapter;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.myinquirt.InquirtBean;
import com.nnfe.fyd.feature.inquiryPrice.AutoInquiryPriceActivity;
import com.nnfe.fyd.feature.login.LoginActivity;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.SharePrefUtil;
import com.nnfe.fyd.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/3/21.
 *
 * 订单
 */

public class MyInquiryFragment_list extends BaseSwipeRefreshFragment {


    @Override
    public Object getTitleString() {
        return "订单";
    }

    @Override
    public void responseSuccess(Call<HttpResult> call, Response<HttpResult> response) {
        if (response.body().status == 0) {
            InquirtBean bean = (InquirtBean) response.body().data;
            setDataList(bean.content);
        }
    }

    @Override
    public MyBaseAdapter getAdapter() {
        return new InquiryAdapter_list(mContext);
    }

    @Override
    public Call<HttpResult> getCall() {
        if (!SharePrefUtil.getBoolean(mContext,ShareKey.isLogin,false)){
            LoginActivity.lauch(mContext,LoginActivity.TYPE_LOGIN);
        }
        return service.inquiryList("" + PAGE, "" + PAGE_SIZE, SharePrefUtil.getString(mContext, ShareKey.UserId));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActionBar.getRightButton().setText("新建");
        mActionBar.getRightButton().setVisibility(View.VISIBLE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        if (str.equalsIgnoreCase(BusKey.POST_inquiryReport)) {
            Log.e("xyd","订单列表有改变");
            onRefresh();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        return rootview;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRightButtonClick() {
        super.onRightButtonClick();
        if (!SharePrefUtil.getBoolean(mContext, ShareKey.isLogin, false)) {
            LoginActivity.lauch(mContext, LoginActivity.TYPE_LOGIN);
        } else {
            checkToken();
        }
    }


    /**
     * 验证token是否有效
     */
    private void checkToken() {
        Call<HttpResult<Boolean>> call = service.checkToken(SharePrefUtil.getString(mContext, ShareKey.UserId, "")
                ,SharePrefUtil.getString(mContext,ShareKey.Token,""));
        call.enqueue(new CallbackImpl<Boolean>() {
            @Override
            public void responseSuccess(Call<HttpResult<Boolean>> call, Response<HttpResult<Boolean>> response) {
                if (response.body().getStatus() == 0){
                    if (response.body().getData()){
                        startActivity(new Intent(mContext, AutoInquiryPriceActivity.class));
                    }else {
                        ToastUtil.showToast("登录失效，请重新登录");
                        SharePrefUtil.clear(mContext);
                        LoginActivity.lauch(mContext,LoginActivity.TYPE_LOGIN);
                    }
                }
            }
        });
    }

    @Override
    public void initLeftButton(Button leftButton) {
        super.initLeftButton(leftButton);
        leftButton.setVisibility(View.GONE);
    }
}
